package com.abigdreamer.im.client.module.login;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

import com.abigdreamer.im.client.module.messageProcess.IMessageProcess;
import com.abigdreamer.im.client.module.network.socket.ClientChatSocket;
import com.abigdreamer.im.client.module.network.socket.ConnectSession;
import com.abigdreamer.im.client.ui.BaseFrame;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.im.message.BaseMessage;
import com.abigdreamer.im.message.LoginResponseMessage;
import com.abigdreamer.im.message.MessageType;
import com.abigdreamer.im.message.OnlineMessage;

public class LoginResponseMessageProcess implements IMessageProcess {

	public void processMessage(BaseMessage message) {
		LoginResponseMessage loginResponse = (LoginResponseMessage) message;

		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				WaitLoginFrame qqWaitLoginFrame = WaitLoginFrame.getInstance();
				qqWaitLoginFrame.setVisible(false);
			}
		});

		int responseInfo = loginResponse.getLoginResponse();
		if (responseInfo == MessageType.LoginResponse_Faile) {
			int errorReason = loginResponse.getErrorReason();
			final String error = EIMClientConfig.getLoginErrrorReasonByType(errorReason);
			Runnable showOptionPane = new Runnable() {
				public void run() {
					EIMLoginWindow loginFrame = EIMLoginWindow.getInstance();
					BaseFrame.centerWindow(loginFrame);
					loginFrame.changTryIcon();
					loginFrame.setVisible(true);
					javax.swing.JOptionPane.showMessageDialog(loginFrame, error);
				}
			};

			try {
				SwingUtilities.invokeAndWait(showOptionPane);
				ClientChatSocket.getInstance().setStart(false);
				ConnectSession.getInstance().close();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		} else {
			// 发送上线消息
			// 扩展:登陆时选择......
			OnlineMessage onlineMessage = new OnlineMessage(message.getDestQQ());
			ConnectSession.getInstance().sendTextMessage(onlineMessage);
		}

	}

}
